﻿using System;
using System.Collections.Generic;
using System.Text;

namespace XAJH
{
    /// <summary>
    /// 鼠标帮助类
    /// </summary>
    public class MouseHelper
    {

        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "SetCursorPos", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private extern static bool SetCursorPos(int x, int y);

        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "ClientToScreen", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        internal extern static bool ClientToScreen(IntPtr hWnd, ref System.Drawing.Point lpPoint);

        #region mouse_event

        /// <summary>
        /// 
        /// </summary>
        /// <param name="dwFlags">事件</param>
        /// <param name="dx">x坐标</param>
        /// <param name="dy">y坐标</param>
        /// <param name="dwData">轮子</param>
        /// <param name="dwExtraInfo">信息</param>
        [System.Runtime.InteropServices.DllImport("user32", EntryPoint = "mouse_event", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private extern static void mouse_event(MouseEventFlag dwFlags, int dx, int dy, uint dwData, IntPtr dwExtraInfo);

        [Flags]
        private enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }

        #endregion

        /// <summary>
        /// 获取或设置鼠标光标的位置（以屏幕坐标表示）。
        /// </summary>
        /// <returns></returns>
        private static System.Drawing.Point MousePosition
        {
            get { return System.Windows.Forms.Control.MousePosition; }
            set { SetCursorPos(value.X, value.Y); }
        }

        /// <summary>
        /// 鼠标移动
        /// </summary>
        /// <param name="hWnd"> 窗口 </param>
        /// <param name="x"> x坐标 </param>
        /// <param name="y"> y坐标 </param>
        public static void MoveTo(IntPtr hWnd, int x, int y)
        {
            //  初始化坐标
            var newPosition = new System.Drawing.Point(x, y);
            //  判断句柄是否有效
            if (hWnd != IntPtr.Zero)
            {
                var pos = newPosition;
                //  转换成屏幕坐标
                if (ClientToScreen(hWnd, ref pos))
                    newPosition = pos;
            }
            //  设置鼠标位置
            MousePosition = newPosition;
        }

        /// <summary>
        /// 鼠标单击
        /// </summary>
        /// <param name="hWnd"> 窗口 </param>
        /// <param name="x"> x坐标 </param>
        /// <param name="y"> y坐标 </param>
        public static void LeftClick(IntPtr hWnd = default (IntPtr), int? x = null, int? y = null)
        {
            //  判断是否提供 x、y 坐标
            if (x != null && y != null)
            {
                //  获取屏幕鼠标当前位置
                var position = System.Windows.Forms.Form.MousePosition;
                //  初始化新的坐标
                var newPosition = new System.Drawing.Point(x.Value, y.Value);
                //  判断是否存在窗口句柄
                if (hWnd != null)
                {
                    //  定义初始坐标
                    var pos = newPosition;
                    //  转换成屏幕坐票
                    if (ClientToScreen(hWnd, ref pos))
                        newPosition = pos;
                }
                //  设置鼠标位置
                if (position != newPosition)
                    MousePosition = new System.Drawing.Point(newPosition.X, newPosition.Y);
            }
            //  模拟鼠标单击
            mouse_event(MouseEventFlag.LeftDown | MouseEventFlag.LeftUp, 0, 0, 0, IntPtr.Zero);
        }
    }
}
